package com.wntms.wy.web;

import com.github.pagehelper.PageInfo;
import com.wntms.wy.pojo.Result;
import com.wntms.wy.pojo.SalesmanAccept;
import com.wntms.wy.pojo.sjhz.DriverBill_s;
import com.wntms.wy.service.SalesmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/salesman")
@CrossOrigin
public class SalesmanCtroller {
    @Autowired
    private SalesmanService salesmanService;
    @GetMapping("/pandaunFC/{fid}")
    public Result pandaunFC(@PathVariable String fid){
        //System.out.println("进来了");
        Boolean aBoolean = salesmanService.panduanFC(fid);//返回true表示有这个货运单
        Result result = new Result();
        if(aBoolean){
            //增加成功
            result.setCode(2001);
            result.setMsg("有该货运单，请继续填写。");
        }else {
            //增加失败
            result.setCode(5001);
            result.setMsg("没有该货运单或已经填写过不能重新填写，请重新输入单号！");
        }

        return result;

    }
    //业务员增加验收，code用2001，失败用5001
    @PostMapping("/salesmanAountAdd")
    public Result salesmanAountAdd(@RequestBody SalesmanAccept salesmanAccept){
        //System.out.println(salesmanAccept);
        Boolean aBoolean = salesmanService.addSalesmanAcount(salesmanAccept);
        Result result = new Result();
        if(aBoolean){
            //增加成功
            result.setCode(2001);
            result.setMsg("增加成功");
        }else {
            //增加失败
            result.setCode(5001);
            result.setMsg("增加失败");
        }

        return result;
    }
    //业务员删除验收
    @DeleteMapping("/salesmanAcountDeleteById/{id}")
    public Result salesmanAcountDeleteById(@PathVariable Integer id){

        Boolean aBoolean = salesmanService.salesmanAcountDeleteById(id);
        Result result = new Result();
        if(aBoolean){
            //删除成功
            result.setCode(2002);
            result.setMsg("删除成功");
        }else {
            //删除失败
            result.setCode(5002);
            result.setMsg("已经有了货运回执单，不能删除！");
        }

        return result;
    }
    //业务员修改验收记录,只能修改ispass,istransit,和arrive_time
    @PutMapping("/salesmanAcountUpdate")
    public Result salesmanAcountUpdate(@RequestBody SalesmanAccept salesmanAccept){
        Boolean aBoolean = salesmanService.salesmanAcountUpdate(salesmanAccept);
        Result result = new Result();
        if(aBoolean){
            //修改成功
            result.setCode(2003);
            result.setMsg("修改成功");
        }else {
            //修改失败
            result.setCode(5003);
            result.setMsg("修改失败");
        }

        return result;
    }

    //查看所有验收记录
    /*@GetMapping("/salesAcountFindAll")
    public Result salesAcountFindAll(){
        List<SalesmanAccept> list = salesmanService.slesmanAcountFindAll();
        Result result = new Result();
        if(list != null) {
            //查询成功

            result.setCode(2004);
            result.setMsg("查询成功");
            result.setData(list);
        }else {
            //查询失败
            //修改成功
            result.setCode(5004);
            result.setMsg("查询失败");
        }
        return result;
    }*/
    @GetMapping("/SAfindByPage/{currentPage}/{pageSize}")
    public Result SAfindByPage(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        List<SalesmanAccept> list = salesmanService.SAfindByPage(currentPage,pageSize);
        System.out.println(list);
        Result result = new Result();
        if(list != null) {
            //查询成功
            PageInfo<SalesmanAccept> pageInfo = new PageInfo<>(list);
            result.setCode(2004);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            //查询失败
            //修改成功
            result.setCode(5004);
            result.setMsg("查询失败");
        }
        return result;
    }
    //给货运单号输入框绑定离焦事件，先判断该货运单是否通过验收，通过验收才能填司机回执单
    @GetMapping("/isSalesmanAccept/{fid}")
    public Result isSalesmanAccept(@PathVariable String fid){
        Boolean aBoolean = salesmanService.panduanByFid(fid);
        Result result = new Result();
        if(aBoolean){
            //增加成功
            result.setCode(2001);
            result.setMsg("有该货运单，且通过验收，请继续填写。");
        }else {
            //增加失败
            result.setCode(5001);
            result.setMsg("无该货运单，或没有通过验收，或已经填写过了，请重新录入单号！");
        }

        return result;

        //return aBoolean;//要是true表示可以填写，false表示没有通过验收或者还没验收
    }

    //业务员填回执单 增

    @PostMapping("/addDriverReceipt/{fid}")
    public Result addDriverReceipt(@PathVariable String fid){
        Boolean aBoolean = salesmanService.addDriverReceipt(fid);
        Result result = new Result();
        if(aBoolean){
            //增加成功
            result.setCode(2002);
            result.setMsg("增加成功");
        }else {
            //增加失败
            result.setCode(5002);
            result.setMsg("增加失败");
        }

        return result;
    }
    //回执表 删
    @DeleteMapping("/deleteDriverReceipt/{id}")
    public Result deleteDriverReceipt(@PathVariable Integer id){
        Boolean aBoolean = salesmanService.deleteDriverReceipt(id);
        Result result = new Result();
        if(aBoolean){
            //删除成功
            result.setCode(2002);
            result.setMsg("删除成功");
        }else {
            //删除失败
            result.setCode(5002);
            result.setMsg("删除失败");
        }

        return result;
    }

    //回执表 改
    @PutMapping("/updateDriverReceipt")
    public Result updateDriverReceipt(@RequestBody DriverBill_s driverBill_s){
        Boolean aBoolean = salesmanService.updateDriverReceipt(driverBill_s);
        Result result = new Result();
        if(aBoolean){
            //修改成功
            result.setCode(2002);
            result.setMsg("修改成功");
        }else {
            //删除失败
            result.setCode(5002);
            result.setMsg("修改失败");
        }

        return result;

    }
    //回执表 查
    @GetMapping("/driverBillsFindAll/{currentPage}/{pageSize}")
    public Result driverBillsFindAll(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        List<DriverBill_s> list = salesmanService.driverBillsFindAll(currentPage,pageSize);
        Result result = new Result();
        if(list != null){
            //修改成功
            PageInfo<DriverBill_s> pageInfo = new PageInfo<>(list);
            result.setCode(2002);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }else {
            //删除失败
            result.setCode(5002);
            result.setMsg("查询失败");
        }

        return result;
    }

}
